﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    class TextMessageFieldControl : UserControl
    {
        private List<string> messages = new List<string>();
        private int maxNumItems;
        private SpriteFont font;
        private int topRecord = 0;

        /// <summary>
        /// Selected message from 0-maxnumbermessages (not relative)
        /// </summary>
        private int selectedItem = -1;

        public override int height
        {
            get
            {
                return _height;
            }
            set
            {
                _height = value;
                Vector2 size = font.MeasureString(" ");
                maxNumItems = height / ((int)size.Y);
            }
        }



        /// <summary>
        /// Loads Content of button
        /// </summary>
        public override void LoadContent()
        {
            base.LoadContent();

            _bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureItemsRegular);
            font = TacticsGame.Instance.gameFont;

            width = 100;
            height = 100;

            isDraggable = true;
        }

        /// <summary>
        /// Updates logic based on mouse
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            List<string> buffer = NetworkMgr.Instance.textMessageBuffer;

            if (buffer.Count > 0)
            {
                for (int i = 0; i < buffer.Count; i += 2)
                {
                    addMessage(buffer[i] + ": " + buffer[i + 1]);
                }
                NetworkMgr.Instance.textMessageBuffer.Clear();
            }

            if (Hover)
            {
                if (InputMgr.Instance.isMouseClickUp(Click.Left))
                {
                    Vector2 size = font.MeasureString(" ");
                    int mx, my;
                    InputMgr.Instance.getMouse(out mx, out my);
                    float unitSize = size.Y;
                    selectedItem = (int)((my - y) / (size.Y));
                    if (selectedItem >= maxNumItems)
                        selectedItem = maxNumItems - 1;

                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            Vector2 size;
            string msg;
            int currentHeight = 0;

            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

            Rectangle targetRect = new Rectangle(x, y, width, height);

            if (Selected)
            {
                TacticsGame.Instance.spriteBatch.Draw(_bgTexture, targetRect, null, Color.White * opacity, 0, Vector2.Zero, SpriteEffects.None, 0);

                //if (selectedItem != -1)
                //{
                //    int unitHeight = (int)((size.Y) * selectedItem);
                //    TacticsGame.Instance.spriteBatch.Draw(bgTexture, new Rectangle(x, y + unitHeight, width, (int)size.Y), null, Color.Black * 0.5f, 0, Vector2.Zero, SpriteEffects.None, 0);
                //}
            }
            else
            {
                TacticsGame.Instance.spriteBatch.Draw(_bgTexture, targetRect, null, Color.White * opacity, 0, Vector2.Zero, SpriteEffects.None, 0);
            }



            for (int i = 0; i < messages.Count - topRecord; i++)
            {
                
                msg = Util.splitString(messages[i + topRecord], width);
                size = font.MeasureString(msg);
                if (size.Y + currentHeight > height)
                    break;

                TacticsGame.Instance.spriteBatch.DrawString(font,
                                            msg,
                                            new Vector2(x, y + currentHeight),
                                            Color.Black * opacity,
                                            0.0f,
                                            new Vector2(0, 0),
                                            Constants.FontScale,
                                            SpriteEffects.None,
                                            0);

                currentHeight += (int)size.Y;

            }



            TacticsGame.Instance.spriteBatch.End();
        }

        public void addMessage(string message)
        {
            messages.Add(message);
            Vector2 size;
            string msg;
            int currentHeight = 0;
            int topAdd = 0;

            for (int i = 0; i < messages.Count - topRecord; i++)
            {
                msg = Util.splitString(messages[i + topRecord], width);
                size = font.MeasureString(msg);
                if (size.Y + currentHeight > height)
                {
                    int temp = msg.Length - msg.Replace("\n", "").Length;
                    if (temp > 1)
                        topAdd += temp;
                    else
                        topAdd += 1;
                }

                currentHeight += (int)size.Y;
            }
            topRecord += topAdd;

        }





    }
}
